home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / util / collections.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  8KB  |  238 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from __future__ import absolute_import
  5. from collections import MutableSequence, defaultdict
  6.  
  7. class DictMixin(object):
  8.     """Implement the dict API using keys() and __*item__ methods.
  9.  
  10.     Similar to UserDict.DictMixin, this takes a class that defines
  11.     __getitem__, __setitem__, __delitem__, and keys(), and turns it
  12.     into a full dict-like object.
  13.  
  14.     UserDict.DictMixin is not suitable for this purpose because it's
  15.     an old-style class.
  16.  
  17.     This class is not optimized for very large dictionaries; many
  18.     functions have linear memory requirements. I recommend you
  19.     override some of these functions if speed is required.
  20.     """
  21.     
  22.     def __iter__(self):
  23.         return iter(self.keys())
  24.  
  25.     
  26.     def has_key(self, key):
  27.         
  28.         try:
  29.             self[key]
  30.         except KeyError:
  31.             return False
  32.  
  33.         return True
  34.  
  35.     __contains__ = has_key
  36.     
  37.     iterkeys = lambda self: iter(self.keys())
  38.     
  39.     def values(self):
  40.         return map(self.__getitem__, self.keys())
  41.  
  42.     
  43.     itervalues = lambda self: iter(self.values())
  44.     
  45.     def items(self):
  46.         return zip(self.keys(), self.values())
  47.  
  48.     
  49.     iteritems = lambda s: iter(s.items())
  50.     
  51.     def clear(self):
  52.         for key in self.keys():
  53.             del self[key]
  54.         
  55.  
  56.     
  57.     def pop(self, key, *args):
  58.         if len(args) > 1:
  59.             raise TypeError('pop takes at most two arguments')
  60.         
  61.         try:
  62.             value = self[key]
  63.         except KeyError:
  64.             if args:
  65.                 return args[0]
  66.  
  67.         del self[key]
  68.         return value
  69.  
  70.     
  71.     def popitem(self):
  72.         
  73.         try:
  74.             key = self.keys()[0]
  75.             return (key, self.pop(key))
  76.         except IndexError:
  77.             raise KeyError('dictionary is empty')
  78.  
  79.  
  80.     
  81.     def update(self, other = None, **kwargs):
  82.         if other is None:
  83.             self.update(kwargs)
  84.             other = { }
  85.         
  86.         try:
  87.             for key, value in other.items():
  88.                 self[key] = value
  89.         except AttributeError:
  90.             for key, value in other:
  91.                 self[key] = value
  92.             
  93.  
  94.  
  95.     
  96.     def setdefault(self, key, default = None):
  97.         
  98.         try:
  99.             return self[key]
  100.         except KeyError:
  101.             self[key] = default
  102.             return default
  103.  
  104.  
  105.     
  106.     def get(self, key, default = None):
  107.         
  108.         try:
  109.             return self[key]
  110.         except KeyError:
  111.             return default
  112.  
  113.  
  114.     
  115.     def __repr__(self):
  116.         return repr(dict(self.items()))
  117.  
  118.     
  119.     def __cmp__(self, other):
  120.         if other is None:
  121.             return 1
  122.         return None(dict(self.items()), other)
  123.  
  124.     __hash__ = object.__hash__
  125.     
  126.     def __len__(self):
  127.         return len(self.keys())
  128.  
  129.  
  130.  
  131. class DictProxy(DictMixin):
  132.     
  133.     def __init__(self, *args, **kwargs):
  134.         self._DictProxy__dict = { }
  135.         super(DictProxy, self).__init__(*args, **kwargs)
  136.  
  137.     
  138.     def __getitem__(self, key):
  139.         return self._DictProxy__dict[key]
  140.  
  141.     
  142.     def __setitem__(self, key, value):
  143.         self._DictProxy__dict[key] = value
  144.  
  145.     
  146.     def __delitem__(self, key):
  147.         del self._DictProxy__dict[key]
  148.  
  149.     
  150.     def keys(self):
  151.         return self._DictProxy__dict.keys()
  152.  
  153.  
  154.  
  155. class HashedList(MutableSequence):
  156.     '''A list-like collection that can only take hashable items
  157.     and provides fast membership tests.
  158.  
  159.     Can handle duplicate entries.
  160.     '''
  161.     
  162.     def __init__(self, arg = None):
  163.         self._map = defaultdict(int)
  164.         if arg is None:
  165.             self._data = []
  166.             return None
  167.         self._data = None(arg)
  168.         for item in arg:
  169.             self._map[item] += 1
  170.         
  171.  
  172.     
  173.     def __setitem__(self, index, item):
  174.         old_items = self._data[index]
  175.         if not isinstance(index, slice):
  176.             old_items = [
  177.                 old_items]
  178.         for old in old_items:
  179.             self._map[old] -= 1
  180.             if not self._map[old]:
  181.                 del self._map[old]
  182.                 continue
  183.         self._data[index] = item
  184.         items = item
  185.         if not isinstance(index, slice):
  186.             items = [
  187.                 items]
  188.         for item in items:
  189.             self._map[item] += 1
  190.         
  191.  
  192.     
  193.     def __getitem__(self, index):
  194.         return self._data[index]
  195.  
  196.     
  197.     def __delitem__(self, index):
  198.         items = self._data[index]
  199.         if not isinstance(index, slice):
  200.             items = [
  201.                 items]
  202.         for item in items:
  203.             self._map[item] -= 1
  204.             if not self._map[item]:
  205.                 del self._map[item]
  206.                 continue
  207.         del self._data[index]
  208.  
  209.     
  210.     def __len__(self):
  211.         return len(self._data)
  212.  
  213.     
  214.     def insert(self, index, item):
  215.         self._data.insert(index, item)
  216.         self._map[item] += 1
  217.  
  218.     
  219.     def __contains__(self, item):
  220.         return item in self._map
  221.  
  222.     
  223.     def __iter__(self):
  224.         for item in self._data:
  225.             yield item
  226.         
  227.  
  228.     
  229.     def has_duplicates(self):
  230.         '''Returns True if any item is contained more then once'''
  231.         return len(self._map) != len(self)
  232.  
  233.     
  234.     def __repr__(self):
  235.         return repr(self._data)
  236.  
  237.  
  238.